package com.mxp.influx.client.controller;

import com.mxp.influx.client.entity.ClientConnection;
import com.mxp.influx.client.entity.api.ApiController;
import com.mxp.influx.client.entity.api.R;
import com.mxp.influx.client.service.ClientConnectionService;
import com.mxp.influx.client.service.InfluxDBService;
import com.mxp.influx.client.utils.PublicUtil;
import org.apache.commons.lang3.StringUtils;
import org.influxdb.InfluxDB;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (ClientConnection)表控制层
 *
 * @author maoxp
 * @since 2024-12-23 11:25:19
 */
@RestController
@RequestMapping("client/connection")
public class ClientConnectionController extends ApiController {
    /**
     * 连接管理服务对象
     */
    @Resource
    private ClientConnectionService clientConnectionService;

    /**
     * InfluxDB服务对象
     */
    @Resource
    private InfluxDBService influxDBService;

    /**
     * 获取连接列表
     * @return
     */
    @GetMapping
    public R list() {
        return success(this.clientConnectionService.list());
    }

    /**
     * 保存更新连接
     * @param clientConnection
     * @return
     */
    @PostMapping
    public R save(@RequestBody ClientConnection clientConnection) {
        Date now = new Date();
        if (StringUtils.isBlank(clientConnection.getId())) {
            clientConnection.setCreateTime(now);
        } else {
            influxDBService.disconnectInfluxDB(clientConnection.getId());
        }
        clientConnection.setUpdateTime(now);
        clientConnectionService.saveOrUpdate(clientConnection);
        return success(clientConnection.getId());
    }

    /**
     * 测试连接
     * @param clientConnection
     * @return
     */
    @PutMapping
    public R test(@RequestBody ClientConnection clientConnection) {
        String pongMsg = influxDBService.testInfluxDB(clientConnection);
        return success(pongMsg, pongMsg);
    }

    /**
     * 删除连接
     * @param id
     * @return
     */
    @DeleteMapping
    public R delete(String id) {
        influxDBService.disconnectInfluxDB(id);
        return success(clientConnectionService.removeById(id));
    }

    /**
     * 获取指定连接下所有数据库
     * @return
     */
    @GetMapping("databases")
    public R databasesList(String connectionId) {
        InfluxDB influxDB = influxDBService.getInfluxDB(connectionId);
        Query query = new Query("SHOW DATABASES");
        QueryResult result = influxDB.query(query);
        return success(PublicUtil.getInfluxDBMapList(result));
    }

    /**
     * 获取指定库下所有表
     * @return
     */
    @GetMapping("measurements")
    public R measurementsList(String connectionId, String database) {
        InfluxDB influxDB = influxDBService.getInfluxDB(connectionId);
        Query query = new Query("SHOW MEASUREMENTS", database);
        QueryResult result = influxDB.query(query);
        return success(PublicUtil.getInfluxDBMapList(result));
    }

}

